/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.github.xwc.example.slice;

import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.PageSliderProvider;

import java.util.List;

/**
 * PageSliderProvider
 *
 * @author xjh
 * @since 2021-04-09
 */
public class PageSliderProviderUtil extends PageSliderProvider {
    private List<Component> mData;
    private List<String> mTabTitle;

    /**
     * 传递参数
     *
     * @param data Component
     * @param tabTitle tabTitle
     */
    public PageSliderProviderUtil(List<Component> data, List<String> tabTitle) {
        this.mData = data;
        this.mTabTitle = tabTitle;
    }

    /**
     * getCount
     *
     * @return Sum Size
     */
    @Override
    public int getCount() {
        return mData.size();
    }

    /**
     * 获取当前createPageInContainer
     *
     * @param componentContainer
     * @param i
     * @return 当前ComponentContainer
     */
    @Override
    public Object createPageInContainer(ComponentContainer componentContainer, int i) {
        int count = componentContainer.getChildCount();
        if (count != 0) {
            for (int index = 0; index < count; index++) {
                Component component = componentContainer.getComponentAt(index);
                Component imageFragment = mData.get(i);
                if (imageFragment.getTag() != component.getTag()) {
                    imageFragment.setTag(i);
                    componentContainer.addComponent(imageFragment);
                }
            }
        } else {
            Component imageFragment = mData.get(i);
            imageFragment.setTag(i);
            componentContainer.addComponent(imageFragment);
        }

        return mData.get(i);
    }

    /**
     * 移除ComponentContainer
     *
     * @param componentContainer
     * @param i
     * @param o
     */
    @Override
    public void destroyPageFromContainer(ComponentContainer componentContainer, int i, Object o) {

    }

    /**
     * 是否匹配
     *
     * @param component
     * @param object
     * @return 是否选中当前界面
     */
    @Override
    public boolean isPageMatchToObject(Component component, Object object) {
        return component == object;
    }

    /**
     * 获取标题
     *
     * @param position
     * @return 获取当前选中界面的title
     */
    @Override
    public String getPageTitle(int position) {
        return mTabTitle.get(position);
    }
}
